package com.huidu.bitrade.service.impl;

import com.huidu.bitrade.constant.TransactionType;
import com.huidu.bitrade.entity.MemberWallet;
import com.huidu.bitrade.entity.OtcCoin;
import com.huidu.bitrade.mapper.AcMemberMapper;
import com.huidu.bitrade.entity.AcMember;
import com.huidu.bitrade.entity.transform.AuthMember;
import com.huidu.bitrade.enums.MemberStarEnum;
import com.huidu.bitrade.service.LocaleMessageSourceService;
import com.huidu.bitrade.service.MemberWalletService;
import com.huidu.bitrade.service.PurchasePayService;
import lombok.Builder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * 金本位
 */
@Service("CnhdPurchaseService")
public class CnhdPurchasePayServiceImpl implements PurchasePayService {


    @Autowired
    private AcMemberMapper acMemberMapper;
    @Autowired
    private MemberWalletService memberWalletService;

    @Autowired
    private LocaleMessageSourceService msService;

    @Override
    public boolean purchase(Integer payType, AuthMember authMember, MemberStarEnum starEnum, Integer purchaseCount, String batchCode, BigDecimal currentHdz) {
        //获得HDC账户余额
        OtcCoin otcCoin = new OtcCoin();
        otcCoin.setUnit("CNHD");
        MemberWallet walletCnhd = memberWalletService.findByOtcCoinAndMemberId(otcCoin, authMember.getId());
        Assert.isTrue(Objects.nonNull(walletCnhd),msService.getMessage("CNHD_NO_ACCOUNT"));
        BigDecimal cnhdBalance = walletCnhd.getBalance();

        //计算所有cnhd
        Data calculate = calculate(payType, authMember, starEnum,cnhdBalance);
        BigDecimal cnhd = calculate.getCnhd();
        acMemberMapper.insert(AcMember.builder()
                .batchCode(batchCode)
                .memberId(authMember.getId())
//                .count(starEnum.getCount())
                .count(purchaseCount)
                .currentHdz(currentHdz)
                .memberStar(starEnum.getLevel())
                .payType(payType)
                .type("CNHD")
                .before(cnhdBalance)
                .number(calculate.getUseCNHD())
                .quantity(cnhd)
                .after(cnhdBalance.subtract(cnhd))
                .percentage(calculate.getCnhdPercentage())
                .build());

        //减去余额并添加流水
        return memberWalletService.operateDeductBalance(walletCnhd,cnhd, TransactionType.ROUTING_BUY);
    }

    @Override
    public void purchaseImport(Integer payType, AuthMember authMember, MemberStarEnum starEnum, Integer purchaseCount, String batchCode, BigDecimal currentHdz) {
        BigDecimal cnhdBalance = BigDecimal.ZERO;

        //计算所有cnhd
        Data calculate = calculate(payType, authMember, starEnum,BigDecimal.valueOf(99999999));
        BigDecimal cnhd = calculate.getCnhd();
        acMemberMapper.insert(AcMember.builder()
                .batchCode(batchCode)
                .memberId(authMember.getId())
//                .count(starEnum.getCount())
                .count(purchaseCount)
                .currentHdz(currentHdz)
                .memberStar(starEnum.getLevel())
                .payType(payType)
                .type("CNHD")
                .before(cnhdBalance)
                .number(calculate.getUseCNHD())
                .quantity(cnhd)
                .after(cnhdBalance.subtract(cnhd))
                .percentage(calculate.getCnhdPercentage())
                .build());
    }

    private Data calculate(Integer payType,AuthMember authMember, MemberStarEnum starEnum,BigDecimal hdcBalance){
        BigDecimal cnhd = BigDecimal.ZERO, cnhdPercentage = BigDecimal.ZERO;

        //用户需要购买的星级减去当前星级cnhd = 本次需要购买的星级所需cnhd
        MemberStarEnum currentStar = authMember.getMemberStarEnum();
        BigDecimal useCNHD = starEnum.getCnhd();
        if(null != currentStar){
            useCNHD = starEnum.getCnhd().subtract(currentStar.getCnhd());
        }

        if(1 == payType){
            //混合支付cnhd占比
            cnhdPercentage = BigDecimal.valueOf(0.3);
            cnhd = useCNHD.multiply(cnhdPercentage);
        }else if(2 == payType){
            //金本位支付
            cnhdPercentage = BigDecimal.valueOf(1);
            cnhd = useCNHD;
        }
        Assert.isTrue(hdcBalance.compareTo(cnhd) >= 0,msService.getMessage("CNHD_INSUFFICIENT_ACCOUNT_BALANCE"));
        return Data.builder()
                .useCNHD(useCNHD)
                .cnhd(cnhd)
                .cnhdPercentage(cnhdPercentage)
                .build();
    }

    @lombok.Data
    @Builder
    static class Data{
        private BigDecimal useCNHD;
        private BigDecimal cnhd;
        private BigDecimal cnhdPercentage;
    }
}
